Skip to contentMethod: lambda$remove$2(Resource, Property, Assertion, String, Value)
1: /**
2: * Copyright (C) 2023 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.jena;
16:
17: import cz.cvut.kbss.ontodriver.descriptor.AbstractAxiomDescriptor;
18: import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
19: import cz.cvut.kbss.ontodriver.jena.util.JenaUtils;
20: import cz.cvut.kbss.ontodriver.model.Assertion;
21: import cz.cvut.kbss.ontodriver.model.NamedResource;
22: import cz.cvut.kbss.ontodriver.model.Value;
23: import org.apache.jena.rdf.model.Property;
24: import org.apache.jena.rdf.model.Resource;
25: import org.apache.jena.rdf.model.ResourceFactory;
26:
27: import java.net.URI;
28: import java.util.Map;
29: import java.util.Set;
30:
31: /**
32: * This class performs an epistemic removal of statements.
33: * <p/>
34: * Epistemic remove means that only information known to the application is deleted. The assertions in the descriptor
35: * represent this information. Thus, only statements representing these properties are removed from the ontology. Note
36: * that if the descriptor contains an unspecified property assertion, all property assertions related to the subject
37: * individual are removed from the property's context.
38: */
39: class EpistemicAxiomRemover {
40:
41: private final StorageConnector connector;
42:
43: EpistemicAxiomRemover(StorageConnector connector) {
44: this.connector = connector;
45: }
46:
47: /**
48: * Removes statements corresponding to the subject and properties specified by the descriptor.
49: *
50: * @param descriptor Descriptor of statements to remove
51: */
52: void remove(AbstractAxiomDescriptor descriptor) {
53: final Resource subject = ResourceFactory.createResource(descriptor.getSubject().getIdentifier().toString());
54: descriptor.getAssertions().forEach(assertion -> {
55: final Property property = ResourceFactory.createProperty(assertion.getIdentifier().toString());
56: if (descriptor.getAssertionContexts(assertion).isEmpty()) {
57: connector.remove(subject, property, null, null);
58: }
59: descriptor.getAssertionContexts(assertion)
60: .forEach(context -> connector.remove(subject, property, null, context.toString()));
61: });
62: }
63:
64: /**
65: * Removes statements corresponding to the specified values.
66: * <p>
67: * This version removes precisely statements whose subject, property and object match the specified data.
68: *
69: * @param subject Statement subject
70: * @param properties Assertions to values
71: * @param context Context from which to remove the statements
72: */
73: void remove(NamedResource subject, Map<Assertion, Set<Value<?>>> properties, URI context) {
74: final Resource resource = ResourceFactory.createResource(subject.getIdentifier().toString());
75: if (context != null) {
76: final String strCtx = context.toString();
77: properties.forEach((assertion, values) -> {
78: final Property property = ResourceFactory.createProperty(assertion.getIdentifier().toString());
79: values.forEach(value -> connector
80: .remove(resource, property, JenaUtils.valueToRdfNode(assertion, value), strCtx));
81: });
82: } else {
83: properties.forEach((assertion, values) -> {
84: final Property property = ResourceFactory.createProperty(assertion.getIdentifier().toString());
85: values.forEach(
86: value -> connector
87: .remove(resource, property, JenaUtils.valueToRdfNode(assertion, value), null));
88: });
89: }
90: }
91: }